package com.example.zhangxin.zyfypt_124zx.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.zhangxin.zyfypt_124zx.R;
import com.example.zhangxin.zyfypt_124zx.adapter.FocusListAdapter;
import com.example.zhangxin.zyfypt_124zx.bean.LoginBean;
import com.example.zhangxin.zyfypt_124zx.bean.UserfocusBean;
import com.example.zhangxin.zyfypt_124zx.iface.FocusListListener;
import com.example.zhangxin.zyfypt_124zx.model.FocusListModel;

import java.util.List;

public class FocusActivity extends AppCompatActivity {

//    @BindView(R.id.recyclerView)
//    RecyclerView recyclerView;

    //private Context context;
    private RecyclerView recyclerView;//recyclerView控件

    private FocusListAdapter adapter;
    private List<UserfocusBean<LoginBean>> list = null;

    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置
    private LinearLayoutManager layoutManager;//显示布局效果

    private String sessionid="";//用户名密码
    private SharedPreferences sp;

    FocusListListener<LoginBean> listener = new FocusListListener<LoginBean>() {
        @Override
        public void onResponse(List<UserfocusBean<LoginBean>> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            if (beanlist.size() != 0) {
                adapter.setList(list);//传给adapter
                adapter.notifyDataSetChanged();//通知更新
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FocusActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_focus);
        //context=getActivity();
       // view=inflater.inflate(R.layout.fragment1,container,false);

        initRecyclerView();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();

        FocusListModel model = new FocusListModel();
        model.getResultList("user", page, sessionid, listener);

        //return view;
    }
    //初始化RecyclerView
    private void initRecyclerView() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        //list=getData();
        //实例化Adapter
        adapter = new FocusListAdapter(this);
        adapter.setList(list);//传递数据
        //绑定recyclerview和adapter
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    FocusListModel model1 = new FocusListModel();
                    model1.getResultList("user", page, sessionid, listener);
                    System.out.println("----onScrollStateChanged  page=" + page);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }

    private void readSP() {
        sessionid=sp.getString("sessionid",null);

    }
}
